<template>
	<view class="page">
		<public-module></public-module>
		<view class="account-form-box">
		<u--form labelPosition="top" :model="model" :rules="rules" ref="uForm" labelWidth="auto">
				<u-form-item :label="$t('t_username')" prop="username">
					<u--input v-model="model.username" :placeholder="$t('pls_username')" type="text" />
				</u-form-item>
				<u-form-item :label="$t('t_pwd')" prop="password">
					<u--input v-model="model.password" :placeholder="$t('desc_strlen')" type="password" />
				</u-form-item>
				<u-form-item :label="$t('t_pwdconfirm')" prop="confirmpassword">
					<u--input v-model="model.confirmpassword" :placeholder="$t('desc_confirmpwd')" type="password" />
				</u-form-item>
				<u-form-item :label="$t('t_invitation')">
					<u--input v-model="model.parentcode" :placeholder="$t('pls_invit')" type="text" />
					<!--text slot="right" class="u-p-l-20">{{$t('pls_invit')}}</text-->
				</u-form-item>
			</u--form>

			<u-gap height="40"></u-gap>

			<u-button type="primary" @click="submit" :disabled="loadstatus" :loading="loadstatus" loadingText="register..">{{$t('register')}}</u-button>

			<u-gap height="10"></u-gap>

			<view class="u-flex u-row-between">
				<view @tap="$tools.goJump('/pages/user/account/login/login')">{{$t('signin_login')}}</view>
			</view>
		</view>

		<u-gap height="40"></u-gap>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
import store from '@/store';
import { api_login,api_register } from '@/config/api.js';
export default {
	data() {
		return {
			loadstatus:false,
			desc: '',
			codeTips: '',
			model: {
				nickname:'',
				username: '',
				/* 密码 */
				password: '',
				/* 确认密码 */
				confirmpassword: '',
				parentcode:'',
				captcha_id:'',
				captcha_code:'',
				position:'',
				reg_channel:''
			},
			rules: {
				 username: [
					{
						required: true,
						message: this.$t('must_username'),
						trigger: ['change', 'blur']
					},
				],
				password: [
					{
						required: true,
						message: this.$t('must_pwd'),
						trigger: ['change', 'blur']
					},
					{
						min: 6,
						max: 20,
						message: this.$t('desc_strlen'),
						trigger: ['change', 'blur']
					},
					// {
					// 	pattern: /^[0-9a-zA-Z]*$/g,
					// 	transform(value) {
					// 		return String(value);
					// 	},
					// 	message: this.$t('desc_pwdnotsati'),
					// 	trigger: ['change', 'blur']
					// }
				],
				confirmPassword: [
					{
						required: true,
						message: this.$t('desc_confirmpwd'),
						trigger: ['change', 'blur']
					},
					{
						validator: (rule, value, callback) => {
							return value === this.model.password;
						},
						message: this.$t('desc_pwddiff'),
						trigger: ['change', 'blur']
					}
				],
			},
			parentcode:''
		};
	},
	computed: {
		...mapState(['userInfo','chatScenesInfo'])
	},
	onLoad(options) {
		// let query=this.userInfo.chatScenesInfo.query;
		// if (query == null || query=="" || query==undefined){}else{
		// 	let querys=JSON.parse(query);
		console.log(this.chatScenesInfo)
		uni.setNavigationBarTitle({title: this.$t('pagetitle.reg')});
		console.log(store.state.chatScenesInfo,'scene--存储参数')
		 	this.model.parentcode=this.chatScenesInfo.salt
		// }
	}, 
	methods: {
		...mapMutations(['setUserInfo']),
		submit() {
			this.$refs.uForm.validate().then(res => {
				this.loadstatus=true
				console.log('aaaa')
				api_register({
					nickname:'',
					username:this.model.username,
					password:this.model.password,
					confirmpassword:this.model.confirmpassword,
					captcha_id:'',
					captcha_code:'',
					parentcode:this.model.parentcode,
					position:'',
					reg_channel:''
				}).then((res) => {
						console.log(res);
						
						this.loadstatus=false
						// 储存登录信息
						let userInfo = {
							...res,
							userId:res.member_id,
							token:true,//token用于判断是否登录
						}
						this.setUserInfo(userInfo)
						uni.showToast({
							title: 'Registration successful~',
							icon: 'none'
						});
						this.loadstatus = false;
						setTimeout(()=>{
							this.$tools.goJump('/pages/my/myInfo',{},'','launch')
						},500)
                }).catch((err) =>{
                	this.loadstatus = false;
                	console.log(err,'catch')
                })
			}).catch(errors => {})
            
		},
		codeChange(text) {
			this.codeTips = text;
		},
		onReady() {
				//onReady 为uni-app支持的生命周期之一
				this.$refs.uForm.setRules(this.rules)
		},

	}
};
</script>

<style lang="scss" scoped>
.account-form-box {
	padding: 0 32rpx;
}
</style>
